package br.com.financeiro.dominio;

import java.io.Serializable;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;

import br.com.financeiro.controllers.PapelMBean;

@Entity
public class Usuario implements Serializable {

	public static final long serialVersionUID = -6011706740724588849L;

	@Id
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_USUARIO")
	@SequenceGenerator(name = "SEQ_USUARIO", sequenceName = "id_seq_usuario", allocationSize = 1)
	@Column(name = "id_usuario")
	private int id;

	private String login;

	private String senha;

	private String nome;

	private boolean ativo;

	@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinTable(name = "permissao", joinColumns = 
	@JoinColumn(name = "id_usuario"), inverseJoinColumns = 
	@JoinColumn(name = "id_papel"))
	private List<Papel> permissoes;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getSenha() {
		return senha;
	}

	public void setSenha(String senha) {
		this.senha = senha;
	}

	public String getNome() {
		return nome;
	}

	public void setNome(String nome) {
		this.nome = nome;
	}

	public boolean isAtivo() {
		return ativo;
	}

	public void setAtivo(boolean ativo) {
		this.ativo = ativo;
	}

	public List<Papel> getPermissoes() {
		return permissoes;
	}

	public void setPermissoes(List<Papel> permissoes) {
		this.permissoes = permissoes;
	}
	
	public void addPermissao(int idPapel) {
		PapelMBean papelMBean = new PapelMBean();
		Papel papel = papelMBean.buscar(idPapel);
		permissoes.add(papel);
	}

}
